<?php
App::uses('AppController', 'Controller');
/**
 * Contacts Controller
 *
 * @property Contact $Contact
 */
class ContactsController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Contact->recursive = 0;
		$this->set('contacts', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Contact->exists($id)) {
			throw new NotFoundException(__('Invalid contact'));
		}
		$options = array('conditions' => array('Contact.' . $this->Contact->primaryKey => $id));
		$this->set('contact', $this->Contact->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Contact->create();
			if ($this->Contact->save($this->request->data)) {
				$this->Session->setFlash(__('The contact has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The contact could not be saved. Please, try again.'));
			}
		}
		$mailLists = $this->Contact->MailList->find('list');
		$this->set(compact('mailLists'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Contact->exists($id)) {
			throw new NotFoundException(__('Invalid contact'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Contact->save($this->request->data)) {
				$this->Session->setFlash(__('The contact has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The contact could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Contact.' . $this->Contact->primaryKey => $id));
			$this->request->data = $this->Contact->find('first', $options);
		}
		$mailLists = $this->Contact->MailList->find('list');
		$this->set(compact('mailLists'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Contact->id = $id;
		if (!$this->Contact->exists()) {
			throw new NotFoundException(__('Invalid contact'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Contact->delete()) {
			$this->Session->setFlash(__('Contact deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Contact was not deleted'));
		$this->redirect(array('action' => 'index'));
	}



    function upload() {
        if (empty($this->data)) {
            // $this->set('tags', $this->Post->Tag->generateList(null,null,null,null,"{n}.Tag.tag"));
            // $this->set('selectedTags', null);
            // $this->render();
        } else {
            // $this->cleanUpFields();

            // upload the file to the server
            $tableName = $this->data['dbtable'];

            // echo $tableName;

            $fileOK = $this->uploadFiles('files', $this->data['csv']);

            // print out data
            // $this->pa($this->data);

            // echo $fileOK['urls'][0];

            $row = 1;
            if (($handle = fopen($fileOK['urls'][0], "r")) !== FALSE) {
                while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) {
                    $num = count($data);
                    if($row != 1 && $data[13] != null) {

                        $this->Contact->create();
                        $this->request->data['Contact']['name'] = $data[1];
                        $this->request->data['Contact']['position'] = $data[2];
                        $this->request->data['Contact']['email'] = $data[3];
                        $this->request->data['Contact']['mobile'] = $data[4];
                        $this->request->data['Contact']['home'] = $data[5];
                        $this->request->data['Contact']['office'] = $data[6];
                        $this->request->data['Contact']['address'] = $data[7];
                        $this->request->data['Contact']['notes'] = $data[8];
                        $this->request->data['Contact']['category'] = $data[9];
                        $this->request->data['Contact']['taken_from'] = $data[10];
                        $this->request->data['Contact']['created_date'] = $data[11];
                        $this->request->data['Contact']['modified_date'] = $data[12];
                        $this->request->data['Contact']['dept_of_cont'] = $data[13];

                        if($this->Contact->save($this->request->data)) {
                            // echo "SUCCESSSSS!";
                        } else {
                            // echo "ERRORRRRRRRRRRR!" ;
                        }


                    }
                    $row++;

                }
                fclose($handle);
            }


            $this->redirect(array('action' => 'result'));

            // $this->pa($this->data['xls']);

            // $this->pa($this->data['xls']['type']);

        }
    }
    function result() {

    }

}
